/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.activiti;

import java.util.HashMap;
import java.util.Map;
import org.apache.camel.Body;
import org.apache.camel.Exchange;
import org.apache.camel.Handler;
import org.apache.camel.Header;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.language.Simple;
import org.springframework.stereotype.Component;

@Component
public class AsyncCamelRoute extends RouteBuilder {
 
    private final Helper helper = new Helper();
    
  @Override
  public void configure() throws Exception {
 
    //Zalozeni
    from("activiti:testWFProcess:zalozeni").setBody(bean(helper)).log("${property.PROCESS_ID_PROPERTY} - Camel zalozeni - SEND")/*.to("log:debug?showAll=true")*/.setHeader("PROCESS_ID_PROPERTY", simple("${property.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.to("activemq:queue:zalozeni1");
    from("activemq:queue:zalozeni1").delay(1000).to("activemq:queue:zalozeni1-2");
    from("activemq:queue:zalozeni1-2")/*.to("log:debug?showAll=true")*/.setProperty("PROCESS_ID_PROPERTY", simple("${header.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.log("${property.PROCESS_ID_PROPERTY} - Camel zalozeni - RECEIVE").to("activiti:testWFProcess:cekaniNaZalozeni");
    
    //Poslat do ukonceni
    from("activiti:testWFProcess:poslatDoUkonceni").setBody(bean(helper)).log("Camel poslatDoUkonceni 1")/*.to("log:debug?showAll=true")*/.setHeader("PROCESS_ID_PROPERTY", simple("${property.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.to("activemq:queue:ukonceni1");
    from("activemq:queue:ukonceni1").delay(1000).to("activemq:queue:ukonceni1-2");
    from("activemq:queue:ukonceni1-2").log("Camel poslatDoUkonceni 2")/*.to("log:debug?showAll=true")*/.setProperty("PROCESS_ID_PROPERTY", simple("${header.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.to("activiti:testWFProcess:cekaniNaUkonceni");
    
    //Rozdeleni 1
    from("activiti:testWFIE:rozdeleni1").setBody(bean(helper)).log("${property.PROCESS_ID_PROPERTY} - Camel rozdeleni1 SEND")/*.to("log:debug?showAll=true")*/.setHeader("PROCESS_ID_PROPERTY", simple("${property.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.to("activemq:queue:rozdeleni1");
    from("activemq:queue:rozdeleni1").delay(1000).to("activemq:queue:rozdeleni1-2");
    from("activemq:queue:rozdeleni1-2")/*.to("log:debug?showAll=true")*/.setProperty("PROCESS_ID_PROPERTY", simple("${header.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.log("${property.PROCESS_ID_PROPERTY} - Camel rozdeleni1 RECEIVE").to("activiti:testWFIE:cekaniNaDokonceniRozdeleni1");
    
    //Rozdeleni 2
    from("activiti:testWFIE:rozdeleni2").setBody(bean(helper)).log("${property.PROCESS_ID_PROPERTY} - Camel rozdeleni2 SEND")/*.to("log:debug?showAll=true")*/.setHeader("PROCESS_ID_PROPERTY", simple("${property.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.to("activemq:queue:rozdeleni2");
    from("activemq:queue:rozdeleni2").delay(1000).to("activemq:queue:rozdeleni2-2");
    from("activemq:queue:rozdeleni2-2")/*.to("log:debug?showAll=true")*/.setProperty("PROCESS_ID_PROPERTY", simple("${header.PROCESS_ID_PROPERTY}"))/*.to("log:debug?showAll=true")*/.log("${property.PROCESS_ID_PROPERTY} - Camel rozdeleni2 RECEIVE").to("activiti:testWFIE:cekaniNaDokonceniRozdeleni2");  
  }
  
  /*
     * A few helper methods used for routing
     */
    public static final class Helper {
        /*
         * This method will extract information from the Exchange (using Camel annotations) and put them in a
         * Map that will be used for setting up the process' variables.
         */
        @Handler
        public Map getProcessVariables(@Body String body,
                                       @Header(Exchange.FILE_NAME) String filename,
                                       @Simple("${date:now:yyyy-MM-dd kk:mm:ss}") String timestamp) {
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("message", "hello");
            variables.put("orderid", "world");
            variables.put("timestamp", 1234566455);
            return variables;
        }
        
        /*@Handler
        public InputStream getProcessVariables(@Body String body,
                                       @Header(Exchange.FILE_NAME) String filename,
                                       @Simple("${date:now:yyyy-MM-dd kk:mm:ss}") String timestamp) {
            String string = "Hello World";
            return new ByteArrayInputStream( string.getBytes() );
        }*/
    }
}